<?php
class AdminUser
{	
	private $db;
	private $table;
	
	
	public function __construct()
	{
		$this->db=new Mysql();
		$this->table=$this->db->pre.'user';
		
	}
	public function getUserList($user)
	{
		//print_r($user);
		$sql = "SELECT * FROM ".$this->table." WHERE 1" ;
		if ($user['isadmin']>1){
			$sql = $sql." AND plantform_id = ".$user['plantform_id'];
		}
		$rs  =  $this->db->getAll($sql);
		$arr = array();
		foreach ($rs as $key=>$value)
		{
			$sql = "SELECT menuid FROM ".$this->db->pre."user_role WHERE uid = ".$value['uid'];
			$res = $this->db->getAll($sql);
			foreach ($res as $k=>$v){
				$arr[]=$v['menuid'];
			}
			$rs[$key]['role']=$arr;
		}
		//print_r($rs);
		return $rs;		
	}
	/**
	 * 
	 * 判断用户是否登录
	 */
	public function isLogin(){
		$user = $this->getUser();
		if ($user['loginStatus']==1){
			return true;
		}else {
			return false;
		}
	}
	/**
	 * 
	 * 通过用户名获取用户资料
	 * @param unknown_type $username
	 */
	public function getUserByUsername($username)
	{
		$sql = "SELECT * FROM ".$this->table."   WHERE username= '$username'";
		$user = $this->db->getRow($sql);
		if ($user){
			$sql = "SELECT menuid FROM ".$this->db->pre."user_role WHERE uid = ".$user['uid'];
			$rs = $this->db->getAll($sql);
			$user['role']=$rs;
			return  $user;
		}else {
			return 0;
		}
	}
	/**
	 * 
	 * 通过email获取用户资料
	 * @param unknown_type $email
	 */
	public function getUserByEmail($email)
	{
		$sql = "SELECT *  FROM ".$this->table." where email like '$email'";
		$user = $this->db->getRow($sql);
		if ($user){
			$sql = "SELECT menuid FROM ".$this->db->pre."user_role WHERE uid = ".$user['uid'];
			$rs = $this->db->getAll($sql);
			$user['role']=$rs;
			return  $user;
		}else {
			return 0;
		}
	}
	/**
	 * 
	 * 通过UID获取用户 资料
	 * @param unknown_type $uid
	 */
	public function getUserByUid($uid)
	{
		$sql = "SELECT * FROM ".$this->table." WHERE  uid = $uid";
		$user = $this->db->getRow($sql);
		$sql = "SELECT menuid FROM ".$this->db->pre."user_role WHERE uid = $uid " ;
		$rs = $this->db->getAll($sql);
		$user['role']=$rs;
		return  $user;
	}
	/**
	 * 
	 * 获取当前登录用户信息
	 */		
	public function getUser()
	{
		$userinfo = getSession('user');
		return $userinfo;		
	}
	
	/**
	 * 
	 * 通过用户检测用户登录是正确
	 * @param unknown_type $username
	 * @param unknown_type $password
	 */
	public function checkUserLoginByUsername($username,$password)
	{		
		$user = $this->getUserByUsername($username);
		if (!$user){
			return -2;
		}
		$password = md5(md5($password).$username);
		//echo $password;
		if ($user['password']==$password){
			$this->sessionUser($user);
			return 1;
		}else{
			return 0;
		}
	}
	/**
	 * 
	 * 通过email检测用户登录是正确
	 * @param unknown_type $email
	 * @param unknown_type $password
	 */
	public function checkUserLoginByEmail($email,$password)
	{		
		$user = $this->getUserByEmail($email);
		if (!$user){
			return -2;
		}
		$password =  md5(md5($password).$user['username']);
		if ($user['password']==$password){
			$this->sessionUser($user);
			return 1;
		}else{
			return 0;
		}
	}
	/**
	 * 
	 * 存储登录状态
	 * @param unknown_type $user
	 */
	public function sessionUser($user)
	{
		$array  =  array('loginStatus'=>1,'userinfo'=>$user);
		setSession('user', $array);
		
	}
	/**
	 * 
	 *重置用户状态
	 */
	public function resessionUser(){
		$user = getSession('user');
		if ($user){
			$userinfo=$this->getUserByUid($user['userinfo']['uid']);
			$array  =  array('loginStatus'=>1,'userinfo'=>$userinfo);
			resetSession('user', $array);
			return true;
		} else {
			return false;
		}
		
	}
}